<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>门店管理</el-breadcrumb-item>
      <el-breadcrumb-item>桌台管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <!--左上功能区-->
    <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd">新增
    </el-button>
    <el-divider></el-divider>
    <!--主体显示列表-->
    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="编号">
      </el-table-column>
      <el-table-column
          prop="tableName"
          label="桌号">
      </el-table-column>
      <el-table-column
          prop="capacity"
          label="容纳人数">
      </el-table-column>
      <el-table-column label="是否启用" width="160" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="toggleEnable(scope.row)"
              v-model="scope.row.isEnable"
              :active-value="0"
              :inactive-value="1"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="150"
          header-align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="text" size="mini" icon="el-icon-edit">编辑
          </el-button>
          <el-button @click="openDeleteConfirm(scope.row)" type="text" icon="el-icon-delete" size="mini"> 删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">

      <!--    新增表单-->
      <el-dialog title="新增" :visible.sync="dialogVisible" width="435px" :before-close="handleClose">
        <el-form :model="form" ref="addForm" :rules="rules" label-width="120px">
          <el-form-item label="桌号" prop="tableName">
            <el-input v-model="form.tableName" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="可容纳人数" prop="capacity">
            <el-input v-model="form.capacity" style="width: 80%"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="insertNew('addForm')">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import BaseUrl from "@/http/BaseUrl";
import TokenAxios from "@/http/TokenAxios";
import axios from "axios";

export default {
  data() {
    return {
      //新增表单显示参数
      dialogVisible: false,
      tableData: [],
      // 新增表单
      form: {},
      rules: {
        tableName: {required: true, message: '请输入库存名称', trigger: 'blur'},
        capacity: [{min: 1, message: '至少容纳1人', trigger: 'blur'}, {
          required: true,
          message: '请输入该桌台可容纳人数',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    //提交表单的时候点到空白处的事件
    handleClose(done) {
      this.$confirm('当前新增桌台表单未提交,确认关闭吗？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
    },
    //新增按钮点击事件
    handleAdd() {
      this.dialogVisible = true
      this.form = {}
    },
    //新增数据
    insertNew(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          let url = BaseUrl.SOTCK + '/table/insert'
          console.log('url = ' + url);
          TokenAxios.post(url, this.form).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state === 20000) {
              this.$message({
                message: '新增桌台成功！',
                type: 'success'
              });
              this.$refs[formName].resetFields();
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    // 点击删除提示
    openDeleteConfirm(row) {
      let message = '此操作将永久删除该数据，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(row);
      }).catch(() => {
      });
    },
    //删除
    handleDelete(row) {
      let url = BaseUrl.SOTCK + '/table/' + row.id + '/delete';
      console.log('url = ' + url);

      TokenAxios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除该数据成功！',
            type: 'success'
          });
          this.getList();
        } else if (jsonResult.state == 50100) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.getList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    //获取列表数据的请求
    getList() {
      let url = BaseUrl.SOTCK + '/table/list';
      console.log('url = ' + url);
      TokenAxios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state === 20000) {
          this.tableData = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    loadIndex() {
      this.getList()
    },
    // 切换启用状态
    toggleEnable(user) {
      let enableText = ['启用', '禁用'];
      let url = BaseUrl.SOTCK + '/table/' + user.id;
      let enable = user.isEnable;
      if (enable == 1) {
        url += '/enable';
      } else {
        url += '/disable';
      }
      console.log('url = ' + url);

      TokenAxios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '将【' + user.tableName + '】的启用状态修改为【' + enableText[enable] + '】成功！',
            type: 'success'
          });
          this.getList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.getList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
        this.getList();
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
  },
  mounted() {
    this.loadIndex()
  }

}
</script>
